package com.mapforge.map;

import java.awt.Graphics2D;
import java.util.Iterator;

import com.mapforge.map.tools.undo.TilePoint;
import com.mapforge.selection.ComparablePoint;
import com.mapforge.selection.Selection;
import com.mapforge.selection.TileSelection;

/**
 * The {@link TileStamp} class represents a saved {@link Selection} of points
 * which correspond to {@link Tile} in a {@link Tileset}. Saved selections can
 * be useful for giving a user a feel for how tiles should be placed, or as
 * convenient quick selections for commonly used patterns of tiles.
 * 
 * @author Will Morrison
 * 
 */
public class TileStamp {
	/**
	 * This {@link TileStamp TileStamp's} {@link TileSelection} of {@link Tile
	 * tiles}.
	 */
	private TileSelection selection;

	/**
	 * {@link Tileset} this {@link TileStamp} is associated with.
	 */
	private Tileset tileset;

	/**
	 * Constructs a new {@link TileStamp} with a specified name and
	 * {@link Selection} of {@link Tile tiles.}
	 * 
	 * @param name
	 *            name of this tilestamp
	 * @param selection
	 *            selection of tiles to use for this tilestamp
	 */
	public TileStamp(TileSelection selection, Tileset tileset) {
		this.selection = selection;
		this.tileset = tileset;
	}

	/**
	 * Returns the tile's which make up this stamp's selection
	 * 
	 * @return this stamp's tile selection
	 */
	public TileSelection getSelection() {
		return this.selection;
	}

	/**
	 * Sets this stamp's selection to the new specified value.
	 * 
	 * @param selection
	 *            stamp's new selection
	 */
	public void setSelection(TileSelection selection) {
		this.selection = selection;
	}

	public void render(Graphics2D g) {
		Iterator<ComparablePoint> it = this.selection.iterator();
		TilePoint p;
		int tw = tileset.getTileWidth();
		int th = tileset.getTileHeight();
		while (it.hasNext()) {
			p = (TilePoint) it.next();
			p.tile.render(g, p.x * tw, p.y * th);
		}
	}
}